#include "monoSampleColumn.h"

void monoSampleColumn(Scene& scene, Viewerf& viewer) {
    Vector3f& pos = viewer.pos;
    Vector3f& frontDir = viewer.dir;
    Vector3f rightDir = frontDir ^ Vector3f(0, 0, 1);
    Vector3f upDir = rightDir ^ frontDir;

    float scale = tan(viewer.fov * 0.5);

    Color3f* colors = new Color3f[viewer.viewsizeY];
    Vector3f* dirs = new Vector3f[viewer.viewsizeY];

    glBegin(GL_POINTS);
        for (int i = 0; i < viewer.viewsizeX; i++) {
            float right = float(i) / viewer.viewsizeX - 0.5;

            Vector3f flatDir = frontDir + right * scale * rightDir;
            flatDir.z = 0;
            flatDir.normalize();

            for (int j = 0 ; j < viewer.viewsizeY; j++) {
                float up = float(j) / viewer.viewsizeY - 0.5;
                Vector3f dir = frontDir + right * scale * rightDir + up * scale * upDir;
                dir.normalize();

                dirs[j] = dir;
            }

            RayColumn rayColumn(pos, flatDir.x, flatDir.y, dirs, viewer.viewsizeY);

            scene.traceRayColumn(colors, rayColumn);

            for (int j = 0 ; j < viewer.viewsizeY; j++) {
                setPixel(i, j, colors[j]);
            }
        }
    glEnd();

    delete[] dirs;
    delete[] colors;
}
